/* 
 * File:   Prueba_socket.cpp
 * Author: saulio
 *
 * Created on April 18, 2012, 12:13 PM
 */


#include"Frame.h"
using namespace std;

/*
 * 
 */
int main() {  
   
     char                        version=65;
     unsigned char               domain=66;
     unsigned int                id_server_source=4000;
     unsigned int                id_server_destination=5000; 
     Header header(version, domain, id_server_source, id_server_destination);
     Membership mb(header);
     public:
    Membership(Header &h)
    {   
        this->header=h.header;
        this->header.type_message=MEMBERSHIP_ID;
        this->header.offset=MEMBERSHIP_OFFSET;
        this->header.flag=NO_FLAG;
 
    }

    static const unsigned char MEMBERSHIP_ID=0x00;
    static const unsigned short MEMBERSHIP_OFFSET=0;
};

     struct msghdr mh;
     struct iovec v[2];
     struct sockaddr_in serv_addr;
     message package;
     int sockfd, n_bytes_write;
     unsigned short Quimera_port=707;
     
     bzero((char *) &serv_addr, sizeof(serv_addr));
     bzero((char *) &package, sizeof(package));
     bzero((char *) &mh, sizeof(mh));
     
     /*header.type_message=0;
     header.version=version;
     header.domain=domain;
     header.flag=0;
     header.offset=0;
     header.id_server_destination=id_server_destination;
     header.id_server_source=id_server_source;*/
     
     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
     v[0].iov_base = &mb.header;
     v[0].iov_len = sizeof(mb.header);
     v[1].iov_base = &package;
     v[1].iov_len = sizeof(package);
     
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(Quimera_port);
     bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr));
     mh.msg_name = &serv_addr;
     mh.msg_namelen = sizeof(serv_addr);
     mh.msg_iov = v;
     mh.msg_iovlen = 2;
     
     n_bytes_write = sendmsg(sockfd, &mh, 0);
            std::cout << int(mb.header.type_message) << std::endl;
            std::cout << int(mb.header.version) << std::endl;
            std::cout << int(mb.header.domain) << std::endl;
            std::cout << int(mb.header.id_server_destination) << std::endl;
            std::cout << int(mb.header.id_server_source) << std::endl;
            std::cout << int(mb.header.flag) << std::endl;
            std::cout << int(mb.header.offset) << std::endl;
     
     close(sockfd);
 }



